package com.hp.team.bookstore.dbutil.init;

import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.hp.team.bookstore.dbutil.dao.DAOFactory;
import com.hp.team.bookstore.dbutil.dao.HibernateUtil;
import com.hp.team.bookstore.dbutil.dao.profile.RoleHibernateDAO;
import com.hp.team.bookstore.dbutil.dao.profile.UserHibernateDAO;
import com.hp.team.bookstore.model.profile.Address;
import com.hp.team.bookstore.model.profile.Gender;
import com.hp.team.bookstore.model.profile.Role;
import com.hp.team.bookstore.model.profile.Telephone;
import com.hp.team.bookstore.model.profile.User;

public class UserInitializer {

	private static UserHibernateDAO userDAO;

	private static RoleHibernateDAO roleDAO;

	static {
		DAOFactory daoFactory = new DAOFactory();
		userDAO = daoFactory.getUserDAO();
		roleDAO = daoFactory.getRoleDAO();
	}

	public static void init() {

		userDAO.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
		HibernateUtil.beginWriteTransaction();

		for (int i = 0; i < 12; i++) {
			User user = createUser(i);
			
			Set<Role> roles = new HashSet<Role>();

			if (i == 0) {
				List<Role> allRoles = roleDAO.findAll();
				roles.addAll(allRoles);
			} else {
				Role role = roleDAO.findById((long) (i / 2) + 1, true);
				roles.add(role);
			}

			user.setRoles(roles);

			userDAO.makePersistent(user);
		}

		HibernateUtil.commitTransaction();
	}

	public static User createUser(long id) {
		Address home = new Address();
		home.setBuildingNumber("Home - Building Number #" + id);
		home.setStreet("Home - Street #" + id);
		home.setState("Home - State #" + id);
		home.setZipCode("Home - Zip #" + id);
		home.setCity("Home - City #" + id);
		home.setCountry("Home - Country #" + id);

		Address office = new Address();
		office.setBuildingNumber("Office - Building Number#" + id);
		office.setStreet("Office - Street #" + id);
		office.setState("Office - State #" + id);
		office.setZipCode("Office - Zip #" + id);
		office.setCity("Office - City #" + id);
		office.setCountry("Office - Country #" + id);

		Telephone homeTele = new Telephone();
		homeTele.setCountryCode("Home Tele - Country Code#" + id);
		homeTele.setNumber("Home Tele - Number #" + id);

		Telephone officeTele = new Telephone();
		officeTele.setCountryCode("Office Tele - Country Code#" + id);
		officeTele.setNumber("Office Tele - Number #" + id);

		Calendar date = Calendar.getInstance();
		date.set(2010, 5, 1);

		User user = new User();
		user.setFirstName("first #" + id);
		user.setLastName("last #" + id);
		user.setAddress(home);
		user.setTelephone(homeTele);
		user.setBusinessAddress(office);
		user.setBusinessTelephone(officeTele);
		user.setEmail("user" + id + "@company.com");
		user.setGender(id % 2 == 0 ? Gender.MALE : Gender.FEMALE);
		user.setHiringDate(date);

		user.setUsername(id == 0 ? "super" : "user" + id);
		user.setPassword(id == 0 ? "super" : "pass" + id);

		return user;
	}

	public static void main(String[] args) {
		UserInitializer.init();
	}

}
